package com.lwm.transactional.annotation.service.impl;


import com.lwm.transactional.annotation.dao.IAccountDAO;
import com.lwm.transactional.annotation.pojo.AccountDO;
import com.lwm.transactional.annotation.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liweimin
 * @date 2021/6/9 19:14
 * @since
 */
@Service
public class AccountServiceImpl implements IAccountService {

    @Autowired
    private IAccountDAO accountDAO;

    @Override
    public List<AccountDO> findAll() {
        return accountDAO.findAll();
    }

    @Override
    public AccountDO findAccountById(Integer id) {
        return accountDAO.findAccountById(id);
    }

    @Override
    public int saveAccount(AccountDO accountDO) {
        return accountDAO.saveAccount(accountDO);
    }

    @Override
    public int deleteAccount(Integer id) {
        return accountDAO.deleteAccount(id);
    }

    @Override
    public int updateAccount(AccountDO accountDO) {
        return accountDAO.updateAccount(accountDO);
    }

    @Override
    public void transfer(String sourceAccount, String targetAccount, Float money) {
        System.out.println("transfer begin 。。。");
        AccountDO account = accountDAO.findAccountByName(sourceAccount);
        AccountDO account1 = accountDAO.findAccountByName(targetAccount);

        account.setMoney(account.getMoney() - money);
        account1.setMoney(account1.getMoney() + money);

        accountDAO.updateAccount(account);
        int i = 1 / 0;
        accountDAO.updateAccount(account1);
    }
}
